﻿@inject IStringLocalizer<Foo> LocalizerFoo

<ValidateForm Model="@Model">
    <EditorForm TModel="Foo" ItemsPerRow="2" RowType="RowType.Inline" LabelAlign="Alignment.Right">
        <FieldItems>
            <EditorItem @bind-Field="@context.Address" ValidateRules="@CustomerRules" />
            <EditorItem @bind-Field="@context.Hobby" Items="@Hobbys" />
        </FieldItems>
    </EditorForm>
</ValidateForm>

@code {
    [NotNull]
    private Foo? Model { get; set; }

    [NotNull]
    private IEnumerable<SelectedItem>? Hobbys { get; set; }

    private List<IValidator> CustomerRules { get; } = new();

    /// <summary>
    /// OnInitialized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        CustomerRules.Add(new FormItemValidator(new EmailAddressAttribute()));
    }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        // 切换线程 模拟异步通过 webapi 加载数据
        await Task.Yield();

        Model = new() { Name = "Name", Education = EnumEducation.Primary, DateTime = DateTime.Now };

        // 初始化参数
        Hobbys = Foo.GenerateHobbys(LocalizerFoo);
    }
}
